﻿/* Show dialog for passed dialog (or id of window)
 * Parameters:
 * dialog - Either an instance of dialog window or ID
 *		of component representing dialog window
 * args - Arguments taht should be passed to window eg.
 *		{foo: 'bar', id: 1}
 */
function showDialog(dialog, args){
	if(typeof(dialog) == 'String'){
		dialog = Ext.getCmp(dialog);
	}
	
	try{
		var url = dialog.url;

		if(args){
			if(typeof(args) == 'String'){
				url = Ext.urlAppend(url, args);
			} else {
				url = Ext.urlAppend(url, Ext.urlEncode(args));
			}
		}

		dialog.loadIFrame({
			url: url,
			showMask: true,
			text: 'Loading...',
			scripts: true
		});

		dialog.show();
		
//		dialog.on('hide', function(el){
//			dialog.un('hide');
//			dialog.fireEvent('dialogclosed');
//			//debugger;
//		});
		
	}catch(e){
		// TODO: Remove for release
		debugger;
		alert('Excpetion showing dialog ' + e.what);
	}
};

/* Confirm action on button before action may proceede
 * button - instance of button to confirm before action
 *		is performed
 */
function allowAction(button){
	if(!button || button.confirmed){
		return;
	}
	
	Ext.Msg.confirm('IDM', button.confirmMsg || 'Proceed?', function(btn){
		button.confirmed = btn == 'yes';
		if(button.confirmed){
			button.fireEvent('click');
			button.confirmed = false;
		}
	});
};

/*
 * Open tab in parent tab panel.
 * Parameters:
 *  url - url of aspx page to open
 *  args -  Arguments taht should be passed to tab eg.
 *		{foo: 'bar', id: 1}
 */
function openTab(url, args){
	var elements = parent.Ext.select('div{tabParent="true"}').elements;
	
	if(!elements.length ){
		return;
	}
	
	if(args){
		if(typeof(args) == 'String'){
			url = Ext.urlAppend(url, args);
		} else {
			url = Ext.urlAppend(url, Ext.urlEncode(args));
		}
	}
	
	var tabPanel = parent.Ext.getCmp(elements[0].id);
	parent.Ext.net.DirectMethods.OpenTab(tabPanel.id, url, tabPanel.items.length);
}